package com.ruoyi.system.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.xiaosiyuan.util.genId.PKGenerator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbWorkHandlerGen;
import com.ruoyi.system.service.ITbWorkHandlerGenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * 代码生成Controller
 * 
 * @author XiaoSiyuan
 * @date 2021-11-11
 */
@RestController
@RequestMapping("/system/workHandlerGen")
public class TbWorkHandlerGenController extends BaseController
{
    @Autowired
    private ITbWorkHandlerGenService tbWorkHandlerGenService;

    /**
     * 查询代码生成列表
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbWorkHandlerGen tbWorkHandlerGen)
    {
        startPage();
        List<TbWorkHandlerGen> list = tbWorkHandlerGenService.selectTbWorkHandlerGenList(tbWorkHandlerGen);
        return getDataTable(list);
    }

    /**
     * 导出代码生成列表
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:export')")
    @Log(title = "代码生成", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbWorkHandlerGen tbWorkHandlerGen)
    {
        List<TbWorkHandlerGen> list = tbWorkHandlerGenService.selectTbWorkHandlerGenList(tbWorkHandlerGen);
        ExcelUtil<TbWorkHandlerGen> util = new ExcelUtil<TbWorkHandlerGen>(TbWorkHandlerGen.class);
        return util.exportExcel(list, "代码生成数据");
    }

    /**
     * 获取代码生成详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbWorkHandlerGenService.selectTbWorkHandlerGenById(id));
    }

    /**
     * 新增代码生成
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:add')")
    @Log(title = "代码生成", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbWorkHandlerGen tbWorkHandlerGen)
    {
        tbWorkHandlerGen.setId(PKGenerator.newId());
        return toAjax(tbWorkHandlerGenService.insertTbWorkHandlerGen(tbWorkHandlerGen));
    }



    /**
     * 修改代码生成
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:edit')")
    @Log(title = "代码生成", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbWorkHandlerGen tbWorkHandlerGen)
    {
        return toAjax(tbWorkHandlerGenService.updateTbWorkHandlerGen(tbWorkHandlerGen));
    }

    /**
     * 删除代码生成
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:remove')")
    @Log(title = "代码生成", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbWorkHandlerGenService.deleteTbWorkHandlerGenByIds(ids));
    }


    /**
     * 复制文件
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:copyFile')")
    @Log(title = "复制文件")
    @PostMapping("/copyFile")
    public AjaxResult copyFile()
    {
        int rs = 0;

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String id = request.getParameter("id");
        TbWorkHandlerGen tbWorkHandlerGen = tbWorkHandlerGenService.selectTbWorkHandlerGenById(id);

        String dbPathAbsolute = tbWorkHandlerGen.getDbPathAbsolute();
        String javaPathPrefix = tbWorkHandlerGen.getJavaPathPrefix();
        String frontPathPrefix = tbWorkHandlerGen.getFrontPathPrefix();


        String zipFilePathPrefix = "C:\\Users\\xiaosiyuan\\Desktop\\ruoyi\\";

        File[] files = FileUtil.ls(zipFilePathPrefix);
        for (File file : files) {
            if (file.isFile()) {
                String sqlPath = dbPathAbsolute+ file.getName();
                FileUtil.copy(file.getPath(), sqlPath, true);
                // 获取不到系统剪切板
//                ClipboardUtil.setStr("source " + sqlPath);

                System.out.println("source " + sqlPath.replaceAll("\\\\", "/"));
            }

            if (file.isDirectory()) {
                if (file.getName().equals("main")) {
                    FileUtil.copy(zipFilePathPrefix + "main\\", javaPathPrefix, true);
                }
                if (file.getName().equals("vue")) {
                    FileUtil.copy(zipFilePathPrefix + "vue\\api\\", frontPathPrefix, true);
                    FileUtil.copy(zipFilePathPrefix + "vue\\views\\", frontPathPrefix, true);
                }
            }
        }

        rs = 200;

        return toAjax(rs);
    }

    /**
     * 移动文件夹
     */
    @PreAuthorize("@ss.hasPermi('system:workHandlerGen:moveDir')")
    @Log(title = "移动文件夹")
    @PostMapping("/moveDir")
    public AjaxResult moveDir()
    {
        int rs = 0;
        try {

            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            request.getParameterMap();

            rs = 200;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return toAjax(rs);
    }

}
